package cn.iocoder.yudao.module.erp.service.business.order.orderproject;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject.vo.OrderProjectPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject.vo.OrderProjectSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderproject.OrderProjectDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.order.orderproject.OrderProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.ORDER_PROJECT_NOT_EXISTS;

/**
 * 项目 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class OrderProjectServiceImpl implements OrderProjectService {

    @Resource
    private OrderProjectMapper orderProjectMapper;

    @Override
    public Long createOrderProject(OrderProjectSaveReqVO createReqVO) {
        // 插入
        OrderProjectDO orderProject = BeanUtils.toBean(createReqVO, OrderProjectDO.class);
        orderProjectMapper.insert(orderProject);
        // 返回
        return orderProject.getId();
    }

    @Override
    public void updateOrderProject(OrderProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderProjectExists(updateReqVO.getId());
        // 更新
        OrderProjectDO updateObj = BeanUtils.toBean(updateReqVO, OrderProjectDO.class);
        orderProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderProject(Long id) {
        // 校验存在
        validateOrderProjectExists(id);
        // 删除
        orderProjectMapper.deleteById(id);
    }

    private void validateOrderProjectExists(Long id) {
        if (orderProjectMapper.selectById(id) == null) {
            throw exception(ORDER_PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public OrderProjectDO getOrderProject(Long id) {
        return orderProjectMapper.selectById(id);
    }

    @Override
    public PageResult<OrderProjectDO> getOrderProjectPage(OrderProjectPageReqVO pageReqVO) {
        return orderProjectMapper.selectPage(pageReqVO);
    }

}